import { getMessage } from '@inbiz/react';
import { useForm } from '@formily/react';
import { DatePicker, Input, InputNumber, Select, Switch } from 'antd';
import { getPlatform, InbizIcon, uuid } from '@inbiz/utils'
import PageSelect from '@inbiz/basic/PageSelect';
import { useQueryFields } from '@inbiz/hooks';
import { Field, Form } from '@formily/core';
import CardTmplSelect from './designer/web/CardTmplSelect';
import { tmplStr } from './designer/web/Tmpl';
import { tmplStr2 } from './designer/web/Tmpl2';
import { tmplStr3 } from './designer/web/Tmpl3';
import operationFlagTitle from './operationFlagTitle';
import { ActionConfigType } from './interfaces';

export const EventsMap = {
  onload: 'onload',
  onSuccess: 'onSuccess',
  onError: 'onError',
  onDelete: 'onDelete',
  onAdd: 'onAdd',
  onEdit: 'onEdit',
  onOpen: 'onOpen',
  onClose: 'onClose',
  onQueryClick: 'onQueryClick',
  onClearClick: 'onClearClick',
  onSaveSearch: 'onSaveSearch',
  onQueryBoxChange: 'onQueryBoxChange',
  onExportParamFormat: 'onExportParamFormat',
  onOperateColumnFormat: 'onOperateColumnFormat',
  onParamFormat: 'onParamFormat',
  onColumnsFormat: 'onColumnsFormat',
  onOperateFormat: 'onOperateFormat',
  onClick: 'onClick',
  onViewClick: 'onViewClick',
  onAfterSort: 'onAfterSort',
  //antd
  onDblClickRow: 'onDblClickRow',
  onSelect: 'onSelect',
  onSelectAll: 'onSelectAll',
  onSelectInvert: 'onSelectInvert',
  onSelectNone: 'onSelectNone',
};
export const MemberTypeKeyMap = {
  0: 'user',
  3: 'group',
  4: 'position',
  5: 'department',
};
export const emptyArr = new Array(8).fill(1);
export enum OperatorEnum {
  eq = 'eq',
  neq = 'neq',
  gt = 'gt',
  gte = 'gte',
  lt = 'lt',
  lte = 'lte',
  like = 'like',
  nlike = 'nlike',
  likein = 'likein',
  include = 'include',
  nstartwith = 'nstartwith',
  startwith = 'startwith',
  endwith = 'endwith',
  nendwith = 'nendwith',
}

export enum ActionEnum {
  add = 'add',
  cancel = 'cancel',
  custom = 'custom',
  customColumn = 'customColumn',
  del = 'del',
  recovery = 'recovery',
  thoroughDel = 'thoroughDel',
  downloadTmpl = 'downloadTmpl',
  edit = 'edit',
  export = 'export',
  import = 'import',
  like = 'like',
  next = 'next',
  pre = 'pre',
  print = 'print',
  save = 'save',
  saveAndNew = 'saveAndNew',
  view = 'view',
  query = 'query',
  reset = 'reset',
  serach = 'search'
}

export enum ConnectorEnum {
  and = 'and',
  or = 'or',
}

export enum UnitEnum {
  percentage = '%',
  pixel = 'px',
}

export enum InputTypeEnum {
  Text = 1,
  Number = 3,
  Date = 4,
  Select = 5,
  Switch = 6,
  CardTmplSelect = 7,
  PageSelect = 8,
}

/**
 * 兼容老版引用方式
 * 老版：import { InbizIcon } from "inbizIcon"
 * 新版：import InbizIcon from "inbizIcon"
 */
let inbizIcon: any = InbizIcon;
inbizIcon.InbizIcon = InbizIcon;

export var cardTmplModules = {
  antd: require('antd'),
  '@ant-design/icons': require('@ant-design/icons'),
  react: require('react'),
  inbizIcon,
};

export const TmplPlaceConfigKey = {
  title: 'title',
  img: 'img',
  dataItems: 'dataItems',
};

export const TmplDefaultConfig = () => ({
  title: {
    placeKey: 'title',
    placeText: '位置1',
    label: '标题',
    typeValue: 'obj',
    propName: '',
  },
  data1: {
    placeKey: 'data1',
    placeText: '位置2',
    label: '数据1',
    typeValue: 'obj',
    propName: '',
  },
  data2: {
    placeKey: 'data2',
    placeText: '位置3',
    label: `数据2`,
    typeValue: 'obj',
    propName: '',
  },
  data3: {
    placeKey: 'data3',
    placeText: '位置4',
    label: `数据3`,
    typeValue: 'array',
    propName: '',
  },
  data4: {
    placeKey: 'data4',
    placeText: '位置5',
    label: `数据4`,
    typeValue: 'obj',
    propName: '',
  },
});

export const TmplDefaultConfig2 = () => ({
  title: {
    placeKey: 'title',
    placeText: '位置1',
    label: '默认模板2-数据1',
    typeValue: 'obj',
    propName: '',
  },
  data1: {
    placeKey: 'data1',
    placeText: '位置2',
    label: '默认模板2-数据2',
    typeValue: 'obj',
    propName: '',
  },
  data2: {
    placeKey: 'data2',
    placeText: '位置3',
    label: '默认模板2-数据3',
    typeValue: 'obj',
    propName: '',
  },
  data3: {
    placeKey: 'data3',
    placeText: '位置4',
    label: '默认模板2-数据4',
    typeValue: 'obj',
    propName: '',
  },
  data4: {
    placeKey: 'data4',
    placeText: '位置5',
    label: '默认模板2-数据5',
    typeValue: 'obj',
    propName: '',
  },
  data5: {
    placeKey: 'data5',
    placeText: '位置6',
    label: '默认模板2-数据6',
    typeValue: 'obj',
    propName: '',
  },
});

export const TmplDefaultConfig3 = () => ({
  img: {
    placeKey: 'img',
    placeText: '位置1',
    label: '默认模板3-图片',
    typeValue: 'obj',
    propName: '',
  },
  data1: {
    placeKey: 'data1',
    placeText: '位置2',
    label: '默认模板3-数据1',
    typeValue: 'obj',
    propName: '',
  },
  data2: {
    placeKey: 'data2',
    placeText: '位置3',
    label: '',
    typeValue: 'obj',
    propName: '',
  },
});

export const builtInTmpl = {
  default: tmplStr,
  default2: tmplStr2,
  default3: tmplStr3,
};

export const ActionPosition = {
  tableHeader: 'tableHeader',
  inline: 'inline',
};

export const SearchConst = {
  all: 'all',
  collapse: 'collapse',
  expanded: 'expanded',
  middle: 'middle',
  right: 'right',
};

export const InputTypeMap: { [key: number]: any } = {
  [InputTypeEnum.Text]: Input,
  [InputTypeEnum.Number]: InputNumber,
  [InputTypeEnum.Date]: DatePicker,
  [InputTypeEnum.Select]: Select,
  [InputTypeEnum.Switch]: Switch,
  [InputTypeEnum.CardTmplSelect]: CardTmplSelect,
  [InputTypeEnum.PageSelect]: PageSelect,
};

export const InlineDefaultActions = ['view', 'edit'];

export const TablePrefixCls = 'inbiz';

export const Actions = () =>
  getPlatform() == 'web'
    ? [
      {
        label: 'Table.button.add',
        value: 'add',
      },
      {
        label: 'Table.button.check',
        value: 'view',
      },
      {
        label: 'Table.button.edit',
        value: 'edit',
      },
      {
        label: 'Table.button.delete',
        value: 'del',
      },
      {
        label: 'Table.button.query',
        value: 'query',
      },
      {
        label: 'Table.button.recovery',
        value: 'recovery',
      },
      {
        label: 'Table.button.thoroughDel',
        value: 'thoroughDel',
      },
      {
        label: 'Table.button.export',
        value: 'export',
      },
      {
        label: 'Table.button.import',
        value: 'import',
      },
      {
        label: 'Table.button.print',
        value: 'print',
      },
      {
        label: 'Table.customColumn',
        value: 'customColumn',
      },
      {
        label: 'Table.button.customize',
        value: 'custom',
      },
    ]
    : [
      {
        label: 'Table.button.add',
        value: 'add',
      },
      {
        label: 'Table.button.check',
        value: 'view',
      },
      {
        label: 'Table.button.edit',
        value: 'edit',
      },
      {
        label: 'Table.button.delete',
        value: 'del',
      },
      {
        label: 'Table.button.customize',
        value: 'custom',
      },
    ];

export const ResConst = {
  other: 'other',
  success: 'success',
  error: 'error',
};
export const Option = () => ({
  actions: Actions(),
  auth: [
    {
      label: 'add',
      value: 'add',
    },
    {
      label: 'edit',
      value: 'edit',
    },
    {
      label: 'delete',
      value: 'del',
    },
  ],
  paramField: [{ label: 'ID', value: 'id' }],
  position: [
    { label: getMessage('Table.head'), value: 'tableHeader' },
    { label: getMessage('Table.inside'), value: 'inline' },
  ],
  openMode: [
    { label: getMessage('Table.popUps'), value: 'modal' },
    { label: `${getMessage('Table.drawer')}-${getMessage('Table.top')}`, value: 'top' },
    {
      label: `${getMessage('Table.drawer')}-${getMessage('Table.bottom')}`,
      value: 'bottom',
    },
    { label: `${getMessage('Table.drawer')}-${getMessage('Table.left')}`, value: 'left' },
    { label: `${getMessage('Table.drawer')}-${getMessage('Table.right')}`, value: 'right' },
  ],
  yesOrNo: [
    { value: '1', label: getMessage('Table.true') },
    { value: '0', label: getMessage('Table.false') },
  ],
  fixed: [
    { value: 'left', label: getMessage('Table.left') },
    { value: 'right', label: getMessage('Table.right') },
    { value: 'none', label: getMessage('Table.false') },
  ],
  sorter: [
    { value: 'on', label: getMessage('Table.open') },
    { value: 'off', label: getMessage('Table.close') },
  ],
  filter: [
    { value: 'on', label: getMessage('Table.open') },
    { value: 'off', label: getMessage('Table.close') },
  ],
  toolbarDisplayPosition: [
    { value: 'left', label: getMessage('Table.left') },
    { value: 'right', label: getMessage('Table.right') },
  ],
  actionDisplaySetup: [
    { value: 'text', label: getMessage('Table.text') },
    { value: 'icon', label: getMessage('Table.icon') },
    { value: 'iconText', label: `${getMessage('Table.icon')}+${getMessage('Table.text')}` },
  ],
  size: [
    { value: 30, label: 30 },
    { value: 60, label: 60 },
    { value: 90, label: 90 },
  ],
  suffixOfUnit: [
    { value: '%', label: '%' },
    { value: 'px', label: 'px' },
  ],
  btnPosition: [
    { value: 'left', label: getMessage('Table.leftAlign') },
    { value: 'center', label: getMessage('Table.centerAlign') },
    { value: 'right', label: getMessage('Table.rightAlign') },
  ],
  pageSize: [
    { value: 10, label: `10${getMessage('Table.strip')}/${getMessage('Table.pageName')}` },
    { value: 20, label: `20${getMessage('Table.strip')}/${getMessage('Table.pageName')}` },
    { value: 30, label: `30${getMessage('Table.strip')}/${getMessage('Table.pageName')}` },
    { value: 50, label: `50${getMessage('Table.strip')}/${getMessage('Table.pageName')}` },
    {
      value: 100,
      label: `100${getMessage('Table.strip')}/${getMessage('Table.pageName')}`,
    },
  ],
  unitOfWidth: [
    { value: 'px', label: getMessage('Table.pixel') },
    { value: '%', label: getMessage('Table.percentage') },
  ],
  parentAction: [
    { value: 'add', label: getMessage('Table.button.add') },
    { value: 'view', label: getMessage('Table.button.check') },
    { value: 'edit', label: getMessage('Table.button.edit') },
    { value: 'query', label: getMessage('Table.button.query') },
  ],
  actionType: [
    { value: 'save', label: 'Table.button.save' },
    { value: 'saveAndNew', label: 'Table.button.saveAndBuild' },
    { value: 'pre', label: 'Table.button.last' },
    { value: 'next', label: 'Table.button.next' },
    { value: 'cancel', label: 'Table.button.cancel' },
    { value: 'custom', label: 'Table.button.customize' },
    { value: 'reset', label: 'Table.button.reset' },
    { value: 'search', label: 'Table.button.search' },
  ],
});

export const ActionTypeObj = () =>
  Option().actionType.reduce((obj, type) => {
    obj[type.value] = type;
    return obj;
  }, {});

export const ColumnConst = {
  index: 'inBizTableIndex',
  actions: 'inBizTableActions',
  sort: 'inBizTableSort',
  subTableIndex: 'inBizSubTableIndex',
  subTableActions: 'inBizSubTableActions',
};
export const arrIsEmpty = (arr: any): boolean => {
  return !arr || !Array.isArray(arr) || arr.length === 0;
};
export const arrIsNotEmpty = (arr: ActionConfigType[] | undefined) => {
  return !arrIsEmpty(arr);
};
export const objIsEmpty = (obj: {}) => {
  return !obj || typeof obj !== 'object' || Object.keys(obj).length === 0;
};
export const objIsNotEmpty = (obj: {}) => {
  return !objIsEmpty(obj);
};

const setSelectProps = (options?: { label: string; value: string }[]) => {
  return {
    type: 'Select',
    options: options?.map((option) => ({ label: getMessage(option.label), value: option.value })),
    allowClear: true,
    defaultOpen: true,
  };
};
const ActionsInHeader = [
  ActionEnum.add,
  ActionEnum.import,
  ActionEnum.export,
  ActionEnum.customColumn,
  ActionEnum.print,
  ActionEnum.query,
];
const ActionsInInline = [ActionEnum.edit, ActionEnum.view];
export const actionHeader = (authOption: any) => {
  const form = useForm();
  const modelKey = (
    form.fields?.['form.senior.soure.x-component-props.sourceModel'] as unknown as Field
  )?.value;
  const sourceType = (form.query(/sourcetype$/).take() as Field)?.value;
  const [fields, loading] = useQueryFields(
    {
      key: modelKey,
      appId: form['appInfo']!.appId,
    },
    [modelKey],
  );
  let optionList: any = [];
  let operatingTypeList: any = Option().actions;
  if (sourceType === 'api') {
    const blackList = [ActionEnum.del, ActionEnum.custom];
    operatingTypeList = operatingTypeList.filter((item) => blackList.includes(item.value));
  }
  if (fields && fields?.length) {
    optionList = fields?.map((ele) => ({ label: ele?.value, value: ele?.value })) || [];
  }
  return getPlatform() == 'web'
    ? [
      {
        title: getMessage('Table.operatingType'),
        dataIndex: 'actionType',
        editData: {
          beforEdit: (record: any) => {
            if (ActionsInInline.includes(record?.actionType)) {
              record.position = 'inline';
            }
            if (ActionsInHeader.includes(record?.actionType)) {
              record.position = 'tableHeader';
            }
            return true;
          },
          afterEdit: (value: any, record: any) => {
            record.actionName = Option().actions.find((option) => option.value === value)?.label;
            if (
              value === ActionEnum.del ||
              value === ActionEnum.thoroughDel ||
              value === ActionEnum.recovery
            ) {
              record.position = 'inline';
            }
            if (value === ActionEnum.custom && !record.operationflag) {
              record.operationflag = uuid(8, 16);
            }
            return record;
          },
          ...setSelectProps(operatingTypeList),
          rules: [
            {
              required: true,
              message: getMessage('Table.tips.mustFillInTheOperationType'),
            },
          ],
        },
        width:100
      },
      {
        title: operationFlagTitle(),
        dataIndex: 'operationflag',
        isIdentical: true,
        width: 150,
        editData: {
          // beforEdit?: (record: any) => boolean | object;
          type: 'AutoComplete',
          // rules: [
          //   {
          //     required: true,
          //   },
          // ],
          options: authOption,
        },
      },
      {
        title: getMessage('Table.operatingName'),
        dataIndex: 'actionName',
        width: 100,
        editData: {
          type: 'MultilingualInput',
          rules: [
            {
              required: true,
            },
          ],
        },
      },
      {
        title: getMessage('Table.configPage'),
        dataIndex: 'page',
        width: 120,
        editData: {
          type: 'PageSelect',
          beforEdit: (record: any) => {
            const blackList = [ActionEnum.add, ActionEnum.edit, ActionEnum.view];
            if (!blackList.includes(record.actionType)) {
              return { ...record };
            }
            return false;
          },
        },
      },
      {
        title: getMessage('Table.parameter'),
        dataIndex: 'paramField',
        width: 100,
        editData: {
          beforEdit: (record: any) => {
            if (record.actionType === 'view' || record.actionType === 'edit') {
              return { ...record };
            }
            return false;
          },
          showSearch: true,
          filterOption: (input, option) => {
            return (option?.children ?.toString()?? '').toLowerCase().includes(input.toLowerCase())
          },
          ...setSelectProps(optionList),
        },
      },
      sourceType != 'model'
        ? {
          title: '数据源',
          dataIndex: 'api',
          width: 100,
          editData: {
            type: 'ApiQuery',
            isDatasource: false,
            open: true,
            beforEdit: (record: { actionType: string }) => {
              return record?.actionType == 'del';
            },
          },
        }
        : undefined,
      {
        title: getMessage('Table.show'),
        dataIndex: 'position',
        width: 100,
        editData: {
          beforEdit: (record: any) => {
            if (ActionsInInline.includes(record?.actionType)) {
              return {
                options: Option().position.filter(({ value }) => value === 'inline'),
              };
            }
            if (ActionsInHeader.includes(record?.actionType)) {
              return {
                options: Option().position.filter(({ value }) => value === 'tableHeader'),
              };
            }
            return true;
          },
          ...setSelectProps(Option().position),
          allowClear: false,
        },
      },
      {
        dataIndex: 'buttonColor',
        width: 150,
        editData: {
          type: 'ColorInput',
          placement: 'bottom',
          isGetPopupContainer: true,
          defualtFocuse: true,
        },
      },
      {
        title: getMessage('Table.buttonIcon'),
        dataIndex: 'icon',
        width: 110,
        editData: {
          type: 'IconPicker',
          showColorPicker: false,
        },
      },
      {
        title: getMessage('Table.openWay'),
        dataIndex: 'openMode',
        width: 100,
        editData: {
          ...setSelectProps(Option().openMode),
        },
      },
      {
        title: getMessage('Table.windowName'),
        dataIndex: 'windowName',
        editData: {
          type: 'MultilingualInput',
        },
      },
    ]
    : [
      {
        title: getMessage('Table.operatingType'),
        dataIndex: 'actionType',
        editData: {
          afterEdit: (value: any, record: any) => {
            record.actionName = Option().actions.find((option) => option.value === value)?.label;
            return record;
          },
          ...setSelectProps(Option().actions),
          rules: [
            {
              required: true,
            },
          ],
        },
      },
      {
        dataIndex: 'operationflag',
        title: operationFlagTitle(),
        isIdentical: true,
        width: 150,
        editData: {
          type: 'AutoComplete',
          options: authOption,
        },
      },
      {
        title: getMessage('Table.operatingName'),
        dataIndex: 'actionName',
        editData: {
          type: 'MultilingualInput',
          rules: [
            {
              required: true,
            },
          ],
        },
      },
      {
        title: getMessage('Table.configPage'),
        dataIndex: 'page',
        width: 200,
        editData: {
          type: 'PageSelect',
        },
      },
    ];
};

export const getPropHeader = (form:any) => {
  const sourceType = (form?.query(/sourcetype$/).take() as Field)?.value;
  const sourceModel = (form?.query(/sourceModel$/).take() as Field)?.value;
  const setAttrComponent = () => {
    //sourceModel 没有配置时就是Input,保证逻辑编排可以用
    if (sourceType == 'api' && typeof sourceModel == 'object' || !sourceModel) {
      return { type: 'Input' };
    }
    return setSelectProps();
  };
  if (getPlatform() == 'web') {
    return [
      {
        title: getMessage('Table.attributeName'),
        dataIndex: 'propName',
        width: 150,
        editData: {
          ...setAttrComponent(),
          rules: [
            {
              required: true,
              message: getMessage('Table.tips.benefitsMustBeFilledIn'),
            },
          ],
        showSearch: true,
        filterOption: (input, option) => {
            return (option?.value ?.toString()?? '').toLowerCase().includes(input.toLowerCase())
        }
        },

      },
      {
        title: getMessage('Table.name'),
        dataIndex: 'columnName',
        width: 200,
        editData: {
          type: 'MultilingualInput',
        },
      },
      {
        title: getMessage('Table.sequence'),
        dataIndex: 'width',
        width: 100,
        editData: {
          type: 'InputNumber',
        },
      },
      {
        title: getMessage('Table.show'),
        dataIndex: 'visible',
        width: 100,
        editData: {
          ...setSelectProps(Option().yesOrNo),
          showSearch: false,
        },
      },
      {
        title: getMessage('Table.startSorting'),
        dataIndex: 'sorter',
        width: 100,
        editData: {
          beforEdit: (record: any) => {
            if (record.fieldType == 'text' || record.fieldType == 'longtext') {
              return false;
            }
            return true;
          },
          ...setSelectProps(Option().sorter),
        },
      },
      // 因需求调整，暂时隐藏列筛选功能
      // {
      //   title: getMessage('Table.startFiltering'),
      //   dataIndex: 'filter',
      //   width: 100,
      //   editData: {
      //     ...setSelectProps(Option().filter),
      //   },
      // },
      {
        title: getMessage('Table.frozenColumn'),
        dataIndex: 'fixed',
        width: 100,
        editData: {
          ...setSelectProps(Option().fixed),
          allowClear: false,
        },
      },
    ];
  }
  return [
    {
      title: getMessage('Table.attributeName'),
      dataIndex: 'propName',
      editData: {
        ...setAttrComponent(),
        rules: [
          {
            required: true,
          },
        ],
        showSearch: true,
        filterOption: (input, option) => {
            return (option?.value ?.toString()?? '').toLowerCase().includes(input.toLowerCase())
        }
      },
    },
    {
      title: getMessage('Table.name'),
      dataIndex: 'columnName',
      editData: {
        type: 'MultilingualInput',
      },
    },
  ];
};

export const ConfigHeader = (form:any) => {
  const sourceType = (form?.query(/sourcetype$/).take() as Field)?.value;
  const sourceModel = (form?.query(/sourceModel$/).take() as Field)?.value;
  const setAttrComponent = () => {
    if (sourceType == 'api' && typeof sourceModel == 'object') {
      return { type: 'Input' };
    }
    return setSelectProps();
  };
  return [
    {
      title: getMessage('Table.displayPosition'),
      dataIndex: 'placeText',
    },
    {
      title: getMessage('Table.tmpTitle'),
      dataIndex: 'label',
      editData: {
        type: 'MultilingualInput',
      },
    },
    {
      title: getMessage('Table.attributeName'),
      dataIndex: 'propName',
      editData: {
        beforEdit: (record: any) => {
          if (record.typeValue != 'array') {
            return true;
          } else {
            return false;
          }
        },
        ...setAttrComponent(),
        showSearch: true,
        filterOption: (input, option) => {
            return (option?.value ?.toString()?? '').toLowerCase().includes(input.toLowerCase())
        }
      },
    },
  ];
};

export const ActionTypeMap = {
  add: [ActionEnum.save, ActionEnum.saveAndNew, ActionEnum.cancel, ActionEnum.custom],
  edit: [
    ActionEnum.save,
    ActionEnum.saveAndNew,
    ActionEnum.pre,
    ActionEnum.next,
    ActionEnum.cancel,
    ActionEnum.custom,
  ],
  view: [ActionEnum.pre, ActionEnum.next, ActionEnum.cancel, ActionEnum.custom],
  query: [ActionEnum.reset, ActionEnum.serach],
};

export const SubWindowHeader = () => [
  {
    title: getMessage('Table.parentLevelOperation'),
    dataIndex: 'parentAction',
    width: 200,
    editData: {
      afterEdit: (value: any, record: any) => {
        const typeOptions = Option().actionType.filter(({ value: parentActionValue }) =>
          ActionTypeMap[value]?.includes(parentActionValue),
        );
        if (!typeOptions.find((option) => option.value === record.actionType)) {
          record.actionType = undefined;
          record.actionName = undefined;
        }
        return record;
      },
      ...setSelectProps(Option().parentAction),
      rules: [
        {
          required: true,
          message: getMessage('Table.tips.parentLevelOperationMustBeFilled'),
        },
      ],
    },
  },
  {
    title: getMessage('Table.operatingType'),
    dataIndex: 'actionType',
    width: 200,
    render: (value: any, record: any) => {
      return getMessage(
        Option().actionType.find((option) => option.value === record.actionType)?.label,
      );
    },
    editData: {
      afterEdit: (value: any, record: any) => {
        record.actionName = Option().actionType.find((option) => option.value === value)?.label;
        return record;
      },
      beforEdit: (record: any) => {
        if (!Object.keys(ActionTypeMap).includes(record?.parentAction)) {
          return {
            options: Option()
            .actionType.map((action) => ({ label: getMessage(action.label), value: action.value }))
          }
        }
        return {
          options: Option()
            .actionType.map((action) => ({ label: getMessage(action.label), value: action.value }))
            .filter(({ value }) => ActionTypeMap[record?.parentAction]?.includes(value)),
        };
      },
      ...setSelectProps(Option().actionType),
      rules: [
        {
          required: true,
        },
      ],
    },
  },
  {
    title: getMessage('Table.operatingName'),
    dataIndex: 'actionName',
    width: 100,
    editData: {
      type: 'MultilingualInput',
    },
  },
];

export const TabsConfig = (form:any) => ({
  actions: {
    table: {
      name: getMessage('Table.operate'),
      formItems:
        getPlatform() == 'web'
          ? [
            {
              name: 'actionColumnWidth',
              label: getMessage('Table.operationColumnWidth'),
              inputType: InputTypeEnum.Number,
            },
            {
              name: 'fixedActionColumn',
              label: getMessage('Table.frozenOperation'),
              inputType: InputTypeEnum.Select,
              options: Option().fixed,
            },
            {
              name: 'tableHeaderBtnShow',
              label: '表头按钮显示',
              inputType: InputTypeEnum.Select,
              defaultValue: 'text',
              options: Option().actionDisplaySetup,
            },
            {
              name: 'inLineBtnShow',
              label: '行内按钮显示',
              inputType: InputTypeEnum.Select,
              defaultValue: 'text',
              options: Option().actionDisplaySetup,
            },
            {
              name: 'pageSelect',
              label: getMessage('Table.pageBinding'),
              inputType: InputTypeEnum.PageSelect,
            },
          ]
          : [],
      // 表头
      header: (authOption) =>
        actionHeader(authOption).filter((col) => col && col.dataIndex !== 'auth'),
    },
    card: {
      name: getMessage('Table.operate'),
      formItems: [
        {
          name: 'tableHeaderBtnShow',
          label: getMessage('Table.tableHeaderBtnDisplay'),
          inputType: InputTypeEnum.Select,
          defaultValue: 'text',
          options: Option().actionDisplaySetup,
        },
        {
          name: 'inLineBtnShow',
          label: getMessage('Table.inLineBtnDisplay'),
          inputType: InputTypeEnum.Select,
          defaultValue: 'text',
          options: Option().actionDisplaySetup,
        },
        {
          name: 'actionPanelWidth',
          label: getMessage('Table.operationPanelWidth'),
          inputType: InputTypeEnum.Number,
        },
        {
          name: 'pageSelect',
          label: getMessage('Table.pageBinding'),
          inputType: InputTypeEnum.PageSelect,
        },
      ],
      header: actionHeader,
    },
  },
  attrs: {
    table: {
      name: getMessage('Table.attribute'),
      formItems:
        getPlatform() == 'web'
          ? [
            {
              name: 'pageSize',
              label: getMessage('Table.pageRecordNumber'),
              inputType: InputTypeEnum.Select,
              defaultValue: 30,
              options: Option().pageSize,
            },
            {
              name: 'widthUnit',
              label: getMessage('Table.columnWidthUnit'),
              inputType: InputTypeEnum.Select,
              options: Option().unitOfWidth,
            },
          ]
          : [
            {
              name: 'pageSize',
              label: getMessage('Table.pageRecordNumber'),
              inputType: InputTypeEnum.Select,
              defaultValue: 10,
              options: Option().pageSize,
            },
            {
              name: 'searchFiled',
              label: getMessage('Table.queryAttribute'),
              inputType: InputTypeEnum.Select,
            },
          ],
      header: getPropHeader(form),
    },
    card: {
      name: getMessage('Table.attribute'),
      hideButton: true,
      formItems: [
        {
          name: 'cardTmpl',
          label: getMessage('Table.selectCardTemplate'),
          inputType: InputTypeEnum.CardTmplSelect,
        },
      ],
      header: () => ConfigHeader(form),
    },
  },
  subWindow: {
    table:
      getPlatform() == 'web'
        ? {
          name: getMessage('Table.subWindow'),
          formItems: [
            {
              name: 'maxHeight',
              label: getMessage('Table.maxHeight'),
              inputType: InputTypeEnum.Number,
              children: [
                {
                  name: 'height',
                  inputType: InputTypeEnum.Number,
                  defaultValue: 60,
                  proportion: '79%',
                },
                {
                  name: 'suffix',
                  inputType: InputTypeEnum.Select,
                  defaultValue: UnitEnum.percentage,
                  options: Option().suffixOfUnit,
                  proportion: '21%',
                },
              ],
            },
            {
              name: 'minHeight',
              label: getMessage('Table.minHeight'),
              inputType: InputTypeEnum.Number,
              children: [
                {
                  name: 'height',
                  inputType: InputTypeEnum.Number,
                  defaultValue: 45,
                  proportion: '79%',
                },
                {
                  name: 'suffix',
                  inputType: InputTypeEnum.Select,
                  defaultValue: UnitEnum.percentage,
                  options: Option().suffixOfUnit,
                  proportion: '21%',
                },
              ],
            },
            {
              name: 'width',
              label: getMessage('Table.windowWidth'),
              inputType: InputTypeEnum.Number,
              children: [
                {
                  name: 'widowWidth',
                  inputType: InputTypeEnum.Number,
                  defaultValue: 45,
                  proportion: '79%',
                },
                {
                  name: 'widthSuffix',
                  inputType: InputTypeEnum.Select,
                  defaultValue: UnitEnum.percentage,
                  options: Option().suffixOfUnit,
                  proportion: '21%',
                },
              ],
            },
            {
              name: 'position',
              label: getMessage('Table.buttonPosition'),
              inputType: InputTypeEnum.Select,
              options: Option().btnPosition,
            },
          ],
          header: SubWindowHeader,
        }
        : {},
    card: {
      name: getMessage('Table.subWindow'),
      formItems: [
        {
          name: 'maxHeight',
          label: getMessage('Table.maxHeight'),
          inputType: InputTypeEnum.Number,
          children: [
            {
              name: 'height',
              inputType: InputTypeEnum.Number,
              defaultValue: 60,
              proportion: '79%',
            },
            {
              name: 'suffix',
              inputType: InputTypeEnum.Select,
              defaultValue: UnitEnum.percentage,
              options: Option().suffixOfUnit,
              proportion: '21%',
            },
          ],
        },
        {
          name: 'minHeight',
          label: getMessage('Table.minHeight'),
          inputType: InputTypeEnum.Number,
          children: [
            {
              name: 'height',
              inputType: InputTypeEnum.Number,
              defaultValue: 45,
              proportion: '79%',
            },
            {
              name: 'suffix',
              inputType: InputTypeEnum.Select,
              defaultValue: UnitEnum.percentage,
              options: Option().suffixOfUnit,
              proportion: '21%',
            },
          ],
        },
        {
          name: 'width',
          label: getMessage('Table.windowWidth'),
          inputType: InputTypeEnum.Number,
          children: [
            {
              name: 'widowWidth',
              inputType: InputTypeEnum.Number,
              defaultValue: 45,
              proportion: '79%',
            },
            {
              name: 'widthSuffix',
              inputType: InputTypeEnum.Select,
              defaultValue: UnitEnum.percentage,
              options: Option().suffixOfUnit,
              proportion: '21%',
            },
          ],
        },
        {
          name: 'position',
          label: getMessage('Table.buttonPosition'),
          inputType: InputTypeEnum.Select,
          options: Option().btnPosition,
        },
      ],
      header: SubWindowHeader,
    },
  },
});
